package com.wuye.web;


import org.json.JSONObject;

public class RespBody<E> implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    public RespBody(ST st) {
        this.code = st.state;
        this.description = st.description;
    }

    public RespBody(E result) {
        this.code = ST.ok.state;
        this.description = ST.ok.description;
        this.result = result;
    }

    public RespBody(ST st, E result) {
        this.code = st.state;
        this.description = st.description;
        this.result = result;
    }

    public int code;
    public String description;
    public E result;
    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public E getResult() {
        return result;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setResult(E result) {
        this.result = result;
    }

    public JSONObject toJSONObject() {
        JSONObject json = new JSONObject();
        if (result != null)
            json.accumulate("result", result);
        json.accumulate("code", code);
        if (description != null)
            json.accumulate("description", description);
        return json;
    }

    @Override
    public String toString() {
        return toJSONObject().toString();
    }

    public boolean ok() {
        return code == ST.ok.state;
    }

}
